package com.link.base.base.auth.service;

import javax.annotation.Resource;

import com.link.base.base.auth.dao.mybatis.mapper.AuthclientMapper;
import com.link.base.base.auth.model.Authclient;
import com.link.core.util.AesUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;

import java.util.List;

/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  黄龙建
 * @date:   2018-09-18
 * @version 1.0
 */

@Service
public class AuthclientServiceImpl extends BasicServiceImpl<Authclient> implements AuthclientService {
    @Resource
    private AuthclientMapper authclientMapper;
    
    @Override
    public BasicMapper<Authclient> getBasicMapper() {
        return authclientMapper;
    }

    /**
     * 获取当前职责的Client
     * @auther HuangLJ
     * @param authclient 职责Id
     * @return
     * @throws Exception 报错
    * @date 2018/9/19 下午6:05
     */
    @Override
    public List<Authclient> queryClientByDutyPage(Authclient authclient) throws Exception {
        return authclientMapper.queryClientByDutyPage(authclient);
    }


    /**
     * @auther HuangLJ
     * @date 2018/9/20 上午10:40
     * @param authclient
     * @return
     * @throws Exception 报错
     * @comments 查询职责下面没有被选中的client
     */
    @Override
    public List<Authclient> queryUnSelectClientListPage(Authclient authclient) throws Exception {
        return authclientMapper.queryUnSelectClientListPage(authclient);
    }

    /**
     * @auther HuangLJ
     * @date 2018/9/20 上午10:40
     * @param authclient
     * @return
     * @throws Exception 报错
     * @comments 通过clientId 查询client
     */
    @Override
    public Authclient queryClientByClientId(Authclient authclient) throws  Exception {
        return authclientMapper.queryClientByClientId(authclient);
    }

    @Override
    public void beforUpsert(Authclient entity) throws Exception {
        if (StringUtils.isBlank(entity.getAesKey())) {
            StringBuilder aesKey = new StringBuilder(AesUtil.generateDesKey(128));
            if (aesKey.length() < 16) {
                while (aesKey.length() < 16) {
                    aesKey.append("0");
                }
            } else {
                aesKey = aesKey.delete(16, aesKey.length());
            }
            entity.setAesKey(aesKey.toString());
        }
    }
}

